﻿using Merge.BLL.Cave;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Merge.BLL
{
    public partial class CaverCell : Form
    {
        public string CaverName { get; set; }
        public int CaverNnumber { get; set; }

        public CaveModel caveModel;
        public CaverCell(string Name,int Num,ref Panel panel,ref CaveModel CaveModel)
        {
            InitializeComponent();
            CaverName = Name;
            CaverNnumber = Num;

            this.caveModel = new CaveModel(CaveModel.CaverModelNumber,
                                            CaveModel.CavePLCMemory_PLC,
                                            CaveModel.HandLeisure_PLC,
                                            CaveModel.CaveStuation_PLC,
                                            CaveModel.Destination_PLC,
                                            CaveModel.Node_PLC,
                                            CaveModel.OldTime);


            this.FormBorderStyle = FormBorderStyle.None;
            this.Size = panel.Size;
            this.BackColor = Color.Lime;
            this.TopLevel = false;
            this.Dock = DockStyle.Fill;
            panel.Controls.Add(this);
            this.Show();

            CaverFormInit(ref CaveModel);
        }




        private void CaverForm_Load(object sender, EventArgs e)
        {

        }
        private void CaverFormInit(ref CaveModel CaveModel)
        {
            CaverPraCountDis(CaveModel.ContProgramme);
        }
        #region 控件方法
        private void CaverPraCountDis(int Val)
        {
            if (Val <= 20)
            {
                Caver_PraCount.Value = Val;
            }

        }

        private void CaverFormColer(CaveModel CaveModel)
        {
            switch (CaveModel.staCaveStuation)
            {
                case 0:
                    CaverSta_Label.Text = CaverName + "待操作";
                    if (CaveModel.Eigenvector.PN !="")
                    {
                        this.BackColor = Color.Lime;
                    }
                    else
                    {
                        this.BackColor = Color.Gray;
                    }
                    break;
                case 1:
                    if (CaveModel.staCavePLCMemory ==100)
                    {
                        this.BackColor = Color.Blue;
                        CaverSta_Label.Text = CaverName + "清仓";
                    }
                    else
                    {
                        this.BackColor = Color.Red;
                        CaverSta_Label.Text = CaverName + "出仓";
                    }
                    break;
                case 2:
                    CaverSta_Label.Text = CaverName + "入仓";
                    this.BackColor = Color.GreenYellow;
                    break;
                case 3:
                    CaverSta_Label.Text = CaverName + "异常处理";
                    this.BackColor = Color.Black;
                    break;
                default:
                    break;
            }
        }

        private void CaverPraCountLabel(int Val)
        {
            Val = Val * 2;
            Cave_CountLabel.Text = Val.ToString();
        }

        private void CaverProCountLabel(int Val)
        {
            CaveProCountLabel.Text = Val.ToString();
        }
        private void CaverClassLabel(CaveModel CaveModel)
        {
            Caver_ClassLabel.Text = Enum.GetName(CaveModel.Eigenvector.LINEi.GetType(), CaveModel.Eigenvector.LINEi) + "\r\n"
            + CaveModel.Eigenvector.PN + "\r\n"
            + Enum.GetName(CaveModel.Eigenvector.Remark.GetType(), CaveModel.Eigenvector.Remark) + "\r\n"
            + Enum.GetName(CaveModel.Eigenvector.KCAndBF.GetType(), CaveModel.Eigenvector.KCAndBF);
        }
        private string ToolTipSNListDis_Moment;
        private void ToolTipSNListDis(CaveModel CaveModel)
        {
            string SNList = "";
            foreach (var item in CaveModel.ListSN)
            {
                SNList += (item + "\r\n");
            }
            if (ToolTipSNListDis_Moment != SNList)
            {
                SnListToolTip.SetToolTip(this, SNList);
                ToolTipSNListDis_Moment = SNList;
            }

        }
        #endregion
        public delegate void CaverFormRefreshDelegate(CaveModel CaveModel);
        public void CaverFormRefresh(CaveModel CaveModel)
        {
            CaverFormColer(CaveModel);
            CaverPraCountDis(CaveModel.staCavePLCMemory * 2);
            CaverPraCountLabel(CaveModel.staCavePLCMemory);
            CaverProCountLabel(CaveModel.ContProgramme);
            CaverClassLabel(CaveModel);
            ToolTipSNListDis(CaveModel);
        }

        private delegate void CaverCellFormDelegate(CaveModel CaveModel);
        private void CaverCellForm(CaveModel CaveModel)
        {
            CaverFormRefreshDelegate caverFormRefresh = new CaverFormRefreshDelegate(CaverFormRefresh);
            this.Invoke(caverFormRefresh, CaveModel);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="CaveModel"></param>
        public void CaverUI(CaveModel CaveModel)
        {
            CaverCellFormDelegate caverCellForm = new CaverCellFormDelegate(CaverCellForm);
            caverCellForm.BeginInvoke(CaveModel, null, null);
        }
        
        private void CaverCell_MouseDown(object sender, MouseEventArgs e)
        {
            
            if (e.Button == MouseButtons.Right)
            {
                switch (CaverNnumber)
                {
                    case 111:
                        Caver.CaveA11.DataClearance();
                        break;
                    case 112:
                        Caver.CaveA12.DataClearance();
                        break;
                    case 113:
                        Caver.CaveA13.DataClearance();
                        break;
                    case 114:
                        Caver.CaveA14.DataClearance();
                        break;
                    case 115:
                        Caver.CaveA15.DataClearance();
                        break;


                    case 121:
                        Caver.CaveA21.DataClearance();
                        break;
                    case 122:
                        Caver.CaveA22.DataClearance();
                        break;
                    case 123:
                        Caver.CaveA23.DataClearance();
                        break;
                    case 124:
                        Caver.CaveA24.DataClearance();
                        break;
                    case 125:
                        Caver.CaveA25.DataClearance();
                        break;

                    case 131:
                        Caver.CaveA31.DataClearance();
                        break;
                    case 132:
                        Caver.CaveA32.DataClearance();
                        break;
                    case 133:
                        Caver.CaveA33.DataClearance();
                        break;
                    case 134:
                        Caver.CaveA34.DataClearance();
                        break;
                    case 135:
                        Caver.CaveA35.DataClearance();
                        break;


                    case 211:
                        Caver.CaveB11.DataClearance();
                        break;
                    case 212:
                        Caver.CaveB12.DataClearance();
                        break;
                    case 213:
                        Caver.CaveB13.DataClearance();
                        break;
                    case 214:
                        Caver.CaveB14.DataClearance();
                        break;
                    case 215:
                        Caver.CaveB15.DataClearance();
                        break;


                    case 221:
                        Caver.CaveB21.DataClearance();
                        break;
                    case 222:
                        Caver.CaveB22.DataClearance();
                        break;
                    case 223:
                        Caver.CaveB23.DataClearance();
                        break;
                    case 224:
                        Caver.CaveB24.DataClearance();
                        break;
                    case 225:
                        Caver.CaveB25.DataClearance();
                        break;

                    case 231:
                        Caver.CaveB31.DataClearance();
                        break;
                    case 232:
                        Caver.CaveB32.DataClearance();
                        break;
                    case 233:
                        Caver.CaveB33.DataClearance();
                        break;
                    case 234:
                        Caver.CaveB34.DataClearance();
                        break;
                    case 235:
                        Caver.CaveB35.DataClearance();
                        break;
                }
            }
        }
    }
}
